home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Networking / AppleTalk Libraries / ZIP.c < prev   
Encoding:
C/C++ Source or Header  |  1991-12-03  |  6.3 KB  |  244 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    ZIP.c : this module contains the Zone Information Protocol functions
  4. #    to get zone names for both AppleTalk Phase 1 and Phase 2.
  5. #
  6. #
  7. #    Versions:    1.0                    10/91
  8. #    Built with MPW 3.2
  9. #
  10. #    C.Buttin - Apple Computer Europe            
  11. #
  12. ------------------------------------------------------------------------------*/
  13. #include <Memory.h>
  14. #include <ToolUtils.h>
  15. #include <Devices.h>
  16. #include <AppleTalk.h>
  17.  
  18.  
  19. /* GetZones :    returns the list of zones
  20.    If Phase 2 driver available 
  21.            issues XPP calls
  22.    else
  23.            issues ATP calls
  24.     Input :
  25.         - a buffer to contain the list
  26.         - buffer size
  27.     Output :
  28.         - total number of zones
  29.                     or
  30.           -1 : buffer was to small
  31.                     or
  32.           error : an error occured */ 
  33.  
  34. pascal short GetZones(Ptr buffer,short bufsize);
  35.  
  36. /* GetZoneName :    Extract a zone name from the buffer list
  37.     Input : 
  38.         - buffer containing the zone list
  39.         - index of the zone to be retrieved
  40.         - Pascal string to receive the zone name (empty string if error) */
  41. pascal void GetZoneName(char* buffer,short zoneNumber,char* zoneName);
  42.  
  43. /* XPP functions */
  44. /* InitXPP : Open the .XPP driver */
  45. pascal OSErr InitXPP(short *driverNum);
  46.  
  47. /* XPPGetZoneList :    returns the list of zones
  48.     Input :
  49.         - a buffer to contain the list
  50.         - buffer size
  51.     Output :
  52.         - total number of zones
  53.                     or
  54.           -1 : buffer was to small
  55.                     or
  56.           error : an error occured */
  57. pascal short XPPGetZoneList(Ptr buffer,short bufsize,short driverNum);
  58.  
  59. /* ATP functions */
  60. /* InitATP : Open the .ATP driver */
  61. pascal OSErr InitATP(void );
  62.  
  63. /* ATPGetZoneList :    returns the list of zones
  64.     Input :
  65.         - a buffer to contain the list
  66.         - buffer size
  67.     Output :
  68.         - total number of zones
  69.                     or
  70.           -1 : buffer was to small
  71.                     or
  72.           error : an error occured */
  73.                 pascal short ATPGetZoneList(Ptr buffer,short bufsize);
  74.  
  75.  
  76.  
  77. pascal short GetZones(Ptr buffer,short bufsize)
  78. {
  79.     SysEnvRec    theConfig;                
  80.     short        driverNum;
  81.     OSErr        error;
  82.  
  83.     SysEnvirons(1, &theConfig);
  84.     if (theConfig.atDrvrVersNum < 53)    {        /* phase 1 calls */
  85.         if ((error = InitATP()) == noErr)
  86.             return(ATPGetZoneList(buffer,bufsize));
  87.         else
  88.             return error;
  89.         }
  90.     else    {                                    /* phase 2 calls */
  91.         if ((error = InitXPP(&driverNum)) == noErr)
  92.             return    XPPGetZoneList(buffer,bufsize,driverNum);
  93.         else
  94.             return error;
  95.             }
  96. }
  97.  
  98.                             /* XPP functions */
  99.                             
  100.                             
  101. pascal OSErr InitXPP(short *driverNum)
  102. {
  103.     OSErr    error;
  104.     
  105.     /* open .MPP driver  and .ATP driver */
  106.     if ((error = InitATP()) != noErr)
  107.         return error;
  108.     /* Open .XPP driver */
  109.     return OpenXPP(driverNum) ;
  110. } /* InitXPP */
  111.  
  112.                             
  113. pascal short XPPGetZoneList(Ptr buffer,short bufsize,short driverNum)
  114. {
  115.     Ptr             theBufferPtr;
  116.     short        numzone;
  117.     short         dataSize;
  118.     Ptr         p;
  119.     OSErr        error;
  120.     XCallParam    XPPBlock;
  121.     
  122.     Debugger();
  123.     
  124.     numzone = 0;
  125.     dataSize = 0;
  126.     p = (Ptr)buffer;
  127.     theBufferPtr = NewPtr(578);     /* ZIP buffer (must be 578 bytes) */                                       
  128.     /* set XPP record */
  129.     XPPBlock.zipInfoField[0] = 0;        /* ALWAYS 0 on first call. contains state info
  130.                                        on subsequent calls */
  131.     XPPBlock.zipInfoField[1] = 0;        /* ALWAYS 0 on first call. contains state info
  132.                                        on subsequent calls */ 
  133.     /* initialization for loop */
  134.        XPPBlock.ioRefNum = driverNum;
  135.     XPPBlock.zipLastFlag = 0;
  136.       XPPBlock.csCode = xCall;
  137.     XPPBlock.xppSubCode = zipGetZoneList;
  138.     XPPBlock.xppTimeout = 3;
  139.     XPPBlock.xppRetry = 4;
  140.     XPPBlock.zipBuffPtr = theBufferPtr;
  141.     do {
  142.         if ((error = GetZoneList((XPPParmBlkPtr)&XPPBlock, false)) != noErr) {
  143.             DisposPtr(theBufferPtr);
  144.             return error;
  145.             }
  146.         /* Get the actual number of zones */
  147.         numzone = numzone + XPPBlock.zipNumZones;
  148.         dataSize = 33 * numzone;            /* 33 char. for each zone */        
  149.         if (bufsize - dataSize <= 0)     {    /* buffer overflow */
  150.             DisposPtr(theBufferPtr);
  151.             return -1;
  152.             }
  153.         /* copy to buffer */
  154.         BlockMove(theBufferPtr,p,(long)33*XPPBlock.zipNumZones);
  155.         p = (Ptr)buffer + dataSize;
  156.         }
  157.     while (XPPBlock.zipLastFlag == 0);     /* ZIP sets user bytes to 0, while all
  158.                                            the data have not been provided */
  159.     DisposPtr(theBufferPtr);
  160.     return numzone;
  161.     
  162. } /* XPPGetZoneList */
  163.  
  164.                             /** ATP functions **/
  165.                             
  166. #pragma segment Main
  167. pascal OSErr InitATP(void)
  168. {
  169.     OSErr error;
  170.     
  171.     if ((error = MPPOpen()) != noErr)
  172.         return error;
  173.     /* open .ATP driver */
  174.     return (ATPLoad());
  175. }    /* InitATP */
  176.  
  177. pascal short ATPGetZoneList(Ptr buffer,short bufsize)
  178. {
  179.     short numzone;
  180.     short node;
  181.     BDSElement bdsp;
  182.     short dataSize;
  183.     ATPParamBlock ATPBlock;            /* to build parameter block for ATP */
  184.     OSErr    error;
  185.  
  186.     /* Get the address of a bridge (node value of the address) */
  187.     if ((node = GetBridgeAddress()) == 0)
  188.         return 0;
  189.     
  190.     dataSize = 0;
  191.     numzone = 0;
  192.     
  193.     /* set ATP record */
  194.  
  195.     ATPBlock.SREQ.ioCompletion = nil;        /* no completion routine */
  196.     ATPBlock.SREQ.addrBlock.aNet = 0;        /* set destination address : current net */
  197.     ATPBlock.SREQ.addrBlock.aNode = node;    /* bridge address */
  198.     ATPBlock.SREQ.addrBlock.aSocket = 6;    /* ZIP socket */
  199.     ATPBlock.SREQ.atpFlags = 0;                /* Control flags : ALO Request */
  200.     ATPBlock.SREQ.filler = 1;               /* numOfBuffs : ZIP expects 1 */   
  201.     ATPBlock.SREQ.timeOutVal = 1;           /* timeout interval (in seconds) */
  202.     ATPBlock.SREQ.retryCount = 5;           /* number of retries */
  203.     ATPBlock.SREQ.reqPointer = nil;         /* number of retries */
  204.     ATPBlock.SREQ.reqLength = 0;           
  205.  
  206.     do {
  207.         if (bufsize - dataSize <= 0)         /* buffer overflow */
  208.             return -1;
  209.  
  210.         bdsp.buffPtr = buffer+dataSize;        /* set the received buffer */
  211.         bdsp.buffSize = bufsize - dataSize;
  212.     
  213.         ATPBlock.SREQ.userData = 0x08000000+numzone+1;    /* specify GetZoneList from zone n */
  214.         ATPBlock.SREQ.bdsPointer = (Ptr)&bdsp;            /* response BDS */   
  215.  
  216.         if ((error = PSendRequest((ATPPBPtr)&ATPBlock,false)) != noErr)
  217.             return error;
  218.     
  219. /* Get the actual number of zones */
  220.         numzone = numzone + LoWord(bdsp.userBytes);
  221.         dataSize = dataSize + bdsp.dataSize;
  222.         }
  223.     while (HiWord(bdsp.userBytes) == 0);     /* ZIP sets user bytes to 0, while all
  224.                                                 the data have not been provided */
  225.     return(numzone);
  226. } /* ATPGetZoneList */
  227.  
  228. pascal void GetZoneName(char* buffer,short zoneNumber,char* zoneName)
  229. {
  230.     short    i,j,lgzone;
  231.     
  232.     for (i = 1; i <= zoneNumber;i++) {
  233.         lgzone = *buffer;
  234.         for (j = 0;j <= lgzone;j++)        /* build zone name */
  235.             if (i == zoneNumber)
  236.                 *zoneName++ = *buffer++;
  237.             else
  238.                 buffer++;
  239.         }
  240. } /* GetZoneName */
  241.  
  242.  
  243.  
  244.